/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.admin;

import com.max.oti.system.model.Charge;
import com.max.oti.system.service.interfac.IChargeService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author max
 */
@ManagedBean
@ViewScoped
public class ChargeBean implements Serializable{
    
    @ManagedProperty("#{chargeService}")
    IChargeService chargeService;
    
    private List<Charge> listCharge;
    private Charge charge = new Charge();
    
    @PostConstruct
    private void initializeLists(){
        try {
            listCharge = chargeService.listHQL("select c.name as name, c.functions as functions from Charge c", Charge.class);
            FacesUtil.addMessage("Carga Completa", null, FacesMessage.SEVERITY_INFO);
        } catch (Exception ex) {
            Logger.getLogger(ChargeBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesUtil.addMessage("Error al cargar los datos", "Cominique al Desarrollador...", FacesMessage.SEVERITY_ERROR);
        }
    }
    
    public void saveOrUpdateCharge() {
        if( charge.getId() == null ){
            try {
                chargeService.save(charge);
                FacesUtil.addMessage("Agregado Con Exito...", null, FacesMessage.SEVERITY_INFO);
                listCharge.add(charge);
                charge = new Charge();
            } catch (Exception ex) {
                FacesUtil.addMessage("Contacte con desarrollador...", null, FacesMessage.SEVERITY_ERROR);
                Logger.getLogger(ChargeBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                chargeService.update(charge);
                FacesUtil.addMessage("Actualizado Con Exito...", null, FacesMessage.SEVERITY_INFO);
                charge = new Charge();
                listCharge = chargeService.listHQL("select c.name as name, c.functions as functions from Charge c", Charge.class);
            } catch (Exception ex) {
                FacesUtil.addMessage("Contacte con desarrollador...", null, FacesMessage.SEVERITY_ERROR);
                Logger.getLogger(ChargeBean.class.getName()).log(Level.SEVERE, null, ex);                
            }
        }
    }
    
    //<editor-fold defaultstate="collapsed" desc="Getter and Setter">
    public void setChargeService(IChargeService chargeService) {
        this.chargeService = chargeService;
    }

    public Charge getCharge() {
        return charge;
    }

    public void setCharge(Charge charge) {
        this.charge = charge;
    }
    
    public List<Charge> getListCharge() {
        return listCharge;
    }

    public void setListCharge(List<Charge> listCharge) {
        this.listCharge = listCharge;
    }
    
    //</editor-fold>

    
}
